package app.temporary;

import app.common.utils.cjt.CJtUtils;
import app.common.utils.cjt.ao.OtherDispatchCreateAo;
import com.alibaba.excel.EasyExcel;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 出库工具类
 * @author feizns
 * @since 2023/08/15
 */
public abstract class OutboundUtilityClass {

    /**
     * 读易excel数据出站形式
     * @return {@link List}<{@link OutboundOrder}>
     */
    public static List<OutboundOrder> readYiDataOutboundFormExcel() {
        return EasyExcel.read(("C:\\Users\\28293\\Desktop\\出库数据-1.xlsx"))
                .head(OutboundOrder.class)
                .doReadAllSync();
    }

    public static void main(String[] args) {
        //错误
        List<Error<OtherDispatchCreateAo.Dto>> errors = new ArrayList<>();
        //出库单
        List<OutboundOrder> orders = readYiDataOutboundFormExcel();
        //出库订单
        orders.stream().filter(order -> order.getDelivery() != null)
                .map(OutboundUtilityClass::outBoundToDto)
                .forEach(dto -> {
                    try {
                        CJtUtils.otherDispatchCreate(dto);
                    } catch (Exception e) {
                        errors.add(new Error<>(dto, e));
                        e.printStackTrace();
                    }
                });
        System.out.println("出库单操作完毕：");
        System.out.println("错误列表");
        List<String> codes = errors.stream()
                .map(Error::getDto)
                .map(OtherDispatchCreateAo.Dto::getCode)
                .collect(Collectors.toList());
        System.out.println(codes);
        errors.forEach(System.out::println);
    }

    /**
     * 出库单,dto
     * @param order 订单
     * @return {@link OtherDispatchCreateAo.Dto}
     */
    public static OtherDispatchCreateAo.Dto outBoundToDto(OutboundOrder order) {
        OtherDispatchCreateAo.Dto dto = new OtherDispatchCreateAo.Dto();
        //字符修改时间
        String stringifyModifyTime = order.stringifyModifyTime();
        String code = StringUtils.hasText(order.getCode()) ? order.getCode() : codeGenerator.code(stringifyModifyTime);
        //生产Code
        dto.setCode(code);
        dto.setExternalCode(code);
        dto.setVoucherType(new OtherDispatchCreateAo.Dto.VoucherType("ST1024"));
        dto.setVoucherDate(stringifyModifyTime);
        dto.setBusiType(new OtherDispatchCreateAo.Dto.BusiType("13"));
        dto.setWarehouse(new OtherDispatchCreateAo.Dto.Warehouse(order.getWarehouseNumber()));
        dto.setMemo(String.format("来源=[宜搭] 表单=[%s] 单号=[%s] 备注：[%s] 原单号：[%s]", order.getSourcesOfForm(), dto.getCode(), order.getNote(), order.getExternalNumber()));
        OtherDispatchCreateAo.Dto.RDRecordDetails detail = new OtherDispatchCreateAo.Dto.RDRecordDetails();
        detail.setInventory(new OtherDispatchCreateAo.Dto.RDRecordDetails.Inventory(order.getMerchantCode()));
        detail.setBaseQuantity(order.getDelivery());
        BigDecimal price = order.getCostPrice();
        detail.setPrice(price);
//        detail.setAmount(NumberUtils.fourDecimalPlaces(order.getCostPrice().multiply(order.getDelivery())));
        detail.setAmount(order.getAmountOf());
        dto.setRDRecordDetailsObj(detail);
        return dto;
    }

    /**
     * 代码生成器
     */
    private static final CodeGenerator codeGenerator = new CodeGenerator("CKY", 3);

}
